package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.BrandService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理品牌相关请求的控制器
 *
 * @author Chen
 */
@Slf4j
@RestController
@RequestMapping("/brands")
@Tag(name = "02. 品牌管理模块")
@Validated
public class BrandController {

    private final BrandService brandService;

    @Autowired
    public BrandController(BrandService brandService) {
        log.debug("创建控制器对象：BrandController");
        this.brandService = brandService;
    }

    // http://localhost:8088/brands/add-new
    @ApiOperationSupport(order = 100)
    @Operation(summary = "添加品牌")
    @PostMapping(value = "/add-new")
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddNewDTO) {
        log.debug("开始处理【添加品牌】的请求，参数：{}", brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/brands/{id}/delete
    @Parameter(name = "id", description = "品牌ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 200)
    @Operation(summary = "根据id删除品牌")
    @PostMapping(value = "/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1L, max = 10000L, message = "删除品牌失败，品牌ID非法！") @PathVariable Long id) {
        log.debug("开始处理【根据id删除品牌】的请求，参数：{}", id);
        brandService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/brands/9527/update
    @ApiOperationSupport(order = 300)
    @Operation(summary = "根据id修改品牌")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【根据id修改品牌详情】的请求，参数ID：{}，新数据：{}", id, brandUpdateDTO);
        brandService.updateInfoById(id, brandUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:8088/brands/9527/enable
    @Parameter(name = "id", description = "品牌ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 310)
    @Operation(summary = "启用品牌")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1L, max = 10000L, message = "启用品牌失败，品牌ID非法！") @PathVariable Long id) {
        log.debug("开始处理【启用品牌】的请求，参数：{}", id);
        brandService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/brands/9527/disable
    @Parameter(name = "id", description = "品牌ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 320)
    @Operation(summary = "禁用品牌")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@Range(min = 1L, max = 10000L, message = "禁用品牌失败，品牌ID非法！") @PathVariable Long id) {
        log.debug("开始处理【禁用品牌】的请求，参数：{}", id);
        brandService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:8088/brands/9527
    @Parameter(name = "id", description = "品牌ID", required = true, example = "9527",
            schema = @Schema(type = "integer", format = "int64"))
    @ApiOperationSupport(order = 410)
    @Operation(summary = "根据id查询品牌详情")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<BrandStandardVO> getStandardById(@Range(min = 1L, max = 10000L, message = "查询品牌详情失败，品牌ID非法！")
                                                           @PathVariable Long id) {
        log.debug("开始处理【根据id查询品牌详情】的请求，参数：{}", id);
        BrandStandardVO brand = brandService.getStandardById(id);
        return JsonResult.ok(brand);
    }

    // http://localhost:8088/brands
    @ApiOperationSupport(order = 420)
    @Operation(summary = "查询品牌列表")
    @GetMapping("")
    public JsonResult<List<BrandListItemVO>> list() {
        log.debug("开始处理【查询品牌列表】的业务，无参数");
        List<BrandListItemVO> list = brandService.list();
        return JsonResult.ok(list);
    }

    @ApiOperationSupport(order = 500)
    @Operation(summary = "重建品牌缓存")
    @GetMapping("/rebuild-cache")
    public JsonResult<Void> rebuildCache() {
        log.debug("开始处理【重建品牌缓存】的业务，无参数");
        brandService.rebuildCache();
        return JsonResult.ok();
    }

}
